package com.quanquan.store.controller;

import DTO.PageDTO;
import com.quanquan.store.dto.IdDTO;
import com.quanquan.store.dto.StoreDTO;
import com.quanquan.store.dto.StoreInfoDTO;
import com.quanquan.store.request.StoreRequest;
import com.quanquan.store.service.ConcernService;
import com.quanquan.store.service.HistoryService;
import com.quanquan.store.service.StoreService;
import enums.ResCodeEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import utils.StringUtils;
import web.response.ResultBody;

import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: Store
 * @Author: xiongjunbao
 * @Date: 2019-03-11 10:29
 * @Version: V1.0
 */
@RestController
@RequestMapping("/store")
public class StoreController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    private final StoreService storeService;
    private final HistoryService historyService;
    private final ConcernService concernService;

    @Autowired
    public StoreController(StoreService storeService, HistoryService historyService, ConcernService concernService) {
        this.storeService = storeService;
        this.historyService = historyService;
        this.concernService = concernService;
    }

    /**
     * 添加商户
     *
     * @param request
     * @return store
     */
    @PostMapping("/add_store")
    public ResultBody addStore(@RequestBody StoreRequest request) {
        if ( StringUtils.isEmpty(request.getStoreName()) || StringUtils.isEmpty(request.getAddress())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "商户信息不完整");
        }
        StoreDTO storeDTO = storeService.saveStore(request);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", storeDTO);
    }

    /**
     * 隐藏、显示商户
     *
     * @param request
     * @return
     */
    @PostMapping("/display_store")
    public ResultBody deleteStore(@RequestBody StoreRequest request) {
        if (request.getStatusShow() == 0) {
            if (StringUtils.isEmpty(request.getId())) {
                return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明要隐藏的商户id");
            }
        }
        if (request.getStatusShow() == 1) {
            if (StringUtils.isEmpty(request.getId())) {
                return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明要显示的商户id");
            }
        }
        Integer mark = storeService.displayStore(request.getId(), request.getStatusShow(), request.getStoreStatus());
        if (mark == 1) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "隐藏商户成功");
        } else if (mark == 0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "显示商户成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "操作失败");
        }

    }

    /**
     * 删除商户
     *
     * @param request
     * @return
     */
    @PostMapping("/remove_store")
    public ResultBody removeStore(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明要删除的商户id");
        }
        Integer mark = storeService.removeStore(request.getId());
        if (mark == 1) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "删除商户成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "操作失败");
        }
    }

    /**
     * 查找商户列表/查找热点商户列表
     *
     * @param request 如果request传参内容包括storeHot，记为搜索热点商户，否则查全部
     * @return
     */
    @PostMapping("/query_list")
    @ResponseBody
    public ResultBody queryList(@RequestBody StoreRequest request) {
        List<StoreDTO> storeDTOList = storeService.storeList(request);
        if (storeDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无商户列表");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", storeDTOList);
    }

    /**
     * 后台查询所有商铺
     * @param request
     * @return
     */
    @PostMapping("/query")
    public ResultBody query(@RequestBody StoreRequest request) {
        PageDTO<StoreDTO> storeDTO = storeService.findAllStore(request);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", storeDTO);
    }

    /**
     * 根据商户id查找商户
     *
     * @param request
     * @return
     */
    @PostMapping("/query_by_id")
    public ResultBody queryById(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "输入的商户id为空");
        }
        StoreDTO storeDTO = storeService.query(request);
        if (storeDTO == null) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无此商户信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", storeDTO);
    }

    /**
     * 修改商户信息llll
     *
     * @param request
     * @return
     */
    @PostMapping("/update_store")
    public ResultBody updateStore(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明id");
        }
        boolean flag = storeService.updateStore(request);
        if (flag) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "更新成功");
        } else {
            return new ResultBody(ResCodeEnum.FAIL.getCode(), "更新失败");
        }
    }

    /**
     * 根据楼层查找对应商户列表
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list_by_floor")
    public ResultBody queryListByFloor(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getFloorName())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "楼层信息为空");
        }
        Integer page = request.getPage() == null ? 0 : request.getPage();
        Integer size = request.getSize() == null ? 100 : request.getSize();
        List<StoreDTO> storeDTOList = storeService.queryListByFloor(page, size, request.getFloorName());
        if (storeDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "此楼层暂无商户信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", storeDTOList);
    }

    /**
     * 通过楼层和业态同时查询商户列表
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list_by_floor_and_commercial")
    public ResultBody queryListByFloorAndCommercial(@RequestBody StoreRequest request) {
        Integer page = request.getPage() == null ? 0 : request.getPage();
        Integer size = request.getSize() == null ? 100 : request.getSize();
        List<StoreDTO> storeDTOList = storeService.queryListByFloorAndCommercial(page, size, request.getFloorName(),request.getCommercialName(),request.getLabelId());
        if (storeDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无内容");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", storeDTOList);
    }

    /**
     * 根据业态查询对应商户
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list_by_commercial")
    public ResultBody queryListByCommercial(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getCommercialName())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "业态名称为空");
        }
        Integer page = request.getPage() == null ? 0 : request.getPage();
        Integer size = request.getSize() == null ? 100 : request.getSize();
        List<StoreDTO> storeDTOList = storeService.queryListByCommercial(page, size, request.getCommercialName());
        if (storeDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "此业态暂无商户信息");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", storeDTOList);
    }

    /**
     * 模糊查询商户列表
     *
     * @param request
     * @return
     */
    @PostMapping("/query_list_by_store_name")
    public ResultBody queryListByStoreName(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getStoreName())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "参数为空");
        }
        String storeName = request.getStoreName().replaceAll(" ", "");
        Integer page = request.getPage() == null ? 0 : request.getPage();
        Integer size = request.getSize() == null ? 100 : request.getSize();
        List<StoreDTO> storeDTOList = storeService.queryListByStoreName(page, size, storeName, request.getType());
        if (storeDTOList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "未搜索到类似名称商户");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", storeDTOList);
    }

    /**
     * 查询历史搜索记录
     *
     * @param request
     * @return
     */
    @PostMapping("/query_histoty")
    public ResultBody queryHistoty(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明用户id");
        }
        List<Object> objectList = storeService.queryByUserId(request);
        if (objectList.size() <= 0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "无记录");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", objectList);
    }

    /**
     * 删除历史搜索记录
     *
     * @param request
     * @return
     */
    @PostMapping("/remove_histoty")
    public ResultBody removeHistoty(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明用户id");
        }
        historyService.removeHistory(request.getUserId());
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "删除搜索历史成功");
    }

    /**
     * 关注商户/取关商户
     *
     * @param request
     * @return
     */
    @PostMapping("/concern_store")
    public ResultBody concernStore(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明用户id");
        }
        if (StringUtils.isEmpty(request.getId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明商户id");
        }
        Integer flag = concernService.concern(request.getUserId(), request.getId());
        if (flag==0) {
            return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "关注商户成功");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "取关商户成功");
    }

    /**
     * 关注商户列表
     * @param request
     * @return
     */
    @PostMapping("/query_concern_list")
    public ResultBody queryConcernList(@RequestBody StoreRequest request) {
        if (StringUtils.isEmpty(request.getUserId())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "未指明用户id");
        }
        List<StoreDTO> storeDTOList = storeService.queryConcernList(request);
        if (storeDTOList.size()<=0) {
            return new ResultBody(ResCodeEnum.NO_DATA.getCode(), "暂无关注商户");
        }
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS",storeDTOList);
    }

    /**
     * 通过idList查找商户列表
     * @param idDTO
     * @return
     */
    @PostMapping("/query_by_id_list")
    @ResponseBody
    public StoreInfoDTO queryByIdList(@RequestBody IdDTO idDTO){

        StoreInfoDTO storeInfoDTO = new StoreInfoDTO();

        storeInfoDTO.setStoreDTOS(storeService.queryByIdLists(idDTO.getForeignKeyIds()));
        return storeInfoDTO;
    }


    /**
     * 通过idList查找商户列表
     * @param idDTO
     * @return
     */
    @PostMapping("/query_by_store_num_list")
    @ResponseBody
    public StoreInfoDTO queryByStoreNumList(@RequestBody IdDTO idDTO){

        StoreInfoDTO storeInfoDTO = new StoreInfoDTO();

        storeInfoDTO.setStoreDTOS(storeService.queryByIdList(idDTO.getForeignKeyIds()));
        return storeInfoDTO;
    }


    @PostMapping("/list")
    public ResultBody list(){
        StoreInfoDTO list = storeService.list();
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS",list);
    }


    //页面条件查询专用
    @PostMapping("/page_find_store")
    public StoreInfoDTO queryStoreByKeyWord  (@RequestBody IdDTO idDTO){
        StoreInfoDTO storeInfoDTO = new StoreInfoDTO();
        List<StoreDTO> listByKeyword = storeService.findListByKeyword(idDTO.getKeyWord());
        storeInfoDTO.setStoreDTOS(listByKeyword);
        return storeInfoDTO;
    }



    @PostMapping("/add")
    public ResultBody createStore (@RequestBody StoreRequest request){
        if ( StringUtils.isEmpty(request.getStoreName()) || StringUtils.isEmpty(request.getAddress())) {
            return new ResultBody(ResCodeEnum.UG_PAEAMES.getCode(), "商户信息不完整");
        }
        StoreDTO storeDTO = storeService.createStore(request);
        return new ResultBody(ResCodeEnum.SUCCESS.getCode(), "SUCCESS", storeDTO);
    }

    /**
     * 根据数采的id获取商户id
     *
     * @param request
     * @return
     */
    @PostMapping("/get_store_id")
    public String getStoreId (@RequestBody StoreRequest request){
        String shopId = storeService.getStoreId(request.getShopEntityId(),request.getShopEntityName());
        return shopId;
    }

}
